package xyz.zdsoft.course_manage_module.service.imp;
import org.springframework.stereotype.Service;
import xyz.zdsoft.common.utils.Result;
import xyz.zdsoft.course_manage_module.mapper.UpdateCourseGchMapper;
import xyz.zdsoft.course_manage_module.service.AdminUpdateCourseService;
import javax.annotation.Resource;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;

@Service
public class AdminUpdateCourseServiceImpl implements AdminUpdateCourseService {
    @Resource
    private UpdateCourseGchMapper updateCourseGchMapper;
    @Resource
    private AdminInsertCourseServiceImpl adminInsertCourseService;
    //通过教室查询当日上课日期(除开自己)
    @Override
    public List<HashMap<String, Object>> SelDateByClassRoom(HashMap<String, Object> map) {
        return updateCourseGchMapper.SelDateByClassRoom(map);
    }
    //删除编辑的课程与实训老师的对应关系
    @Override
    public void DeleteCourseAndTeacher(Integer courseOnId) {
        updateCourseGchMapper.DeleteCourseAndTeacher(courseOnId);
    }
    //更新课程安排信息
    @Override
    public void UpdateCourse(HashMap<String, Object> map) {
        updateCourseGchMapper.UpdateCourse(map);

    }
    //总体逻辑判断
    public Result judgeUpdateCourse(Integer courseOnId,String projectDate, String courseName,
                                    Long dutyTeacherId, Long[] trainTeachers,
                                    String signInTime, String signOutTime,
                                    String classRome, String remark) throws ParseException {
        HashMap<String,Object> courseMap=new HashMap<>();
        courseMap.put("courseOnId",courseOnId);
        courseMap.put("projectDate",projectDate);
        courseMap.put("dutyTeacherId",dutyTeacherId);
        courseMap.put("classRome",classRome);
        courseMap.put("signInTime",signInTime);
        courseMap.put("signOutTime",signOutTime);
        courseMap.put("remark",remark);

        if (adminInsertCourseService.updateTime(projectDate, signInTime) > 0) {//判断时间是否过期
            List<HashMap<String,Object>> dateMaps=updateCourseGchMapper.SelDateByClassRoom(courseMap);//获取当期教室上课时间
            if (adminInsertCourseService.judgeDateTime(dateMaps,classRome, projectDate, signInTime,signOutTime) > 0) {//时间冲突判断
                if (adminInsertCourseService.judgeInAndOutTime(signInTime,signOutTime)>0) {//判断签退时间是否在签到时间之前
                    if (adminInsertCourseService.haveTeacher(trainTeachers) == 0) {//判断教师是否存在/是否停业
                        if (adminInsertCourseService.dutyTeacherIdInTrainTeachers(dutyTeacherId, trainTeachers) > 0) {//判断责任老师是否在实训老师之中
                            Integer courseId = adminInsertCourseService.SelCourseId(courseName);//获取新增的课程id
                            courseMap.put("courseId",courseId);
                            updateCourseGchMapper.UpdateCourse(courseMap);          //更新课程安排信息
                            updateCourseGchMapper.DeleteCourseAndTeacher(courseOnId);  //删除课程安排与实训老师对应关系
                            for (Long trainTeacher : trainTeachers) {
                                adminInsertCourseService.InsertCourseAndTrainTeacher(courseOnId, trainTeacher);//新增课程安排与实训老师对应关系
                            }
                            return Result.ok().message("更新成功");
                        } else return Result.error().message("责任老师必须在实训老师之中");
                    } else return Result.error().message("ID为:" + adminInsertCourseService.haveTeacher(trainTeachers) + "的教师不存在或已被停业");
                }else return Result.error().message("签退时间不能早于签到时间");
            }
            else return Result.error().message("该教室在该时间内已被占用");
        }
        else return Result.error().message("该时间已过期");
    }

}
